package com.maila.xiaoaoschool.adapter;

import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.ShopActivity;
import com.maila.xiaoaoschool.entity.ShopClasses;
import com.maila.xiaoaoschool.util.Constants;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.NumberFormat;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;


public class GoodsAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private List<ShopClasses.DataBean.GoodBean> dataList;
    private ShopActivity mContext;
    private NumberFormat nf;
    private LayoutInflater mInflater;

    public GoodsAdapter(List<ShopClasses.DataBean.GoodBean> dataList, ShopActivity mContext) {
        this.dataList = dataList;
        this.mContext = mContext;
        nf = NumberFormat.getCurrencyInstance();
        nf.setMaximumFractionDigits(2);
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDataList(List<ShopClasses.DataBean.GoodBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if(convertView==null) {
            convertView = mInflater.inflate(R.layout.item_header_view, parent, false);
        }
        AutoUtils.auto(convertView);
        ((TextView)convertView.findViewById(R.id.tv_category_name)).setText(dataList.get(position).getTypename());

        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return dataList.get(position).getType();
    }

    @Override
    public int getCount() {
        if(dataList==null){
            return 0;
        }
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder = null;
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.item_goods,parent,false);
            holder = new ItemViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);
        }else{
            holder = (ItemViewHolder) convertView.getTag();
        }
        ShopClasses.DataBean.GoodBean item = dataList.get(position);
        holder.bindData(item);
        return convertView;
    }

    class ItemViewHolder implements View.OnClickListener{
        private TextView name,price,tvCount,fee,tip,sellcount;
        ImageView tvAdd,tvMinus,goodimg;
        private ShopClasses.DataBean.GoodBean item;


        public ItemViewHolder(View itemView) {
            name = (TextView) itemView.findViewById(R.id.tvName);
            price = (TextView) itemView.findViewById(R.id.tvPrice);
            fee = (TextView) itemView.findViewById(R.id.tv_fee);
            tip = (TextView) itemView.findViewById(R.id.tv_tip);
            sellcount = (TextView) itemView.findViewById(R.id.tv_xiaoliang);
            tvCount = (TextView) itemView.findViewById(R.id.count);
            tvMinus = (ImageView) itemView.findViewById(R.id.tvMinus);
            tvAdd = (ImageView) itemView.findViewById(R.id.tvAdd);
            goodimg= (ImageView) itemView.findViewById(R.id.img);

            tvMinus.setOnClickListener(this);
            tvAdd.setOnClickListener(this);
        }

        public void bindData(ShopClasses.DataBean.GoodBean item){
            this.item = item;
            name.setText(item.getGoodsname());
            item.setBuynum(mContext.getSelectedItemCountById(item.getGoodsid()));
            tvCount.setText(String.valueOf(item.getBuynum()));
            if(item.getIs_cx()==1){
                price.setText(nf.format(item.getCxcost()));
                fee.setVisibility(View.VISIBLE);
                fee.setText(nf.format(item.getCost()));
                fee.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            }else {
                price.setText(nf.format(item.getCost()));
                fee.setVisibility(View.GONE);
            }
            sellcount.setText("月销量："+item.getSellcount()+"份");
            tip.setVisibility(View.VISIBLE);
            if(item.getIs_cx()==1){
                tip.setText(item.getCxzhe()+"折  限"+item.getCxnum()+"份");
            }else if(item.getIs_new()==1){
                tip.setText("今日特价");
            }else {
                tip.setVisibility(View.GONE);
            }
            Glide.with(mContext).load(Uri.parse(Constants.URL_ROOT_XIAOAO+item.getImg())).into(goodimg);
            if(item.getBuynum()<1){
                tvCount.setVisibility(View.GONE);
                tvMinus.setVisibility(View.GONE);
            }else{
                tvCount.setVisibility(View.VISIBLE);
                tvMinus.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onClick(View v) {
            ShopActivity activity = mContext;
            switch (v.getId()){
                case R.id.tvAdd: {
                    int count = activity.getSelectedItemCountById(item.getGoodsid());
                    if (count < 1) {
                        tvMinus.setAnimation(getShowAnimation());
                        tvMinus.setVisibility(View.VISIBLE);
                        tvCount.setVisibility(View.VISIBLE);
                    }
                    activity.add(item, false);
                    count++;
                    tvCount.setText(String.valueOf(count));
                    int[] loc = new int[2];
                    v.getLocationInWindow(loc);
                    activity.playAnimation(loc);
                }
                break;
                case R.id.tvMinus: {
                    int count = activity.getSelectedItemCountById(item.getGoodsid());
                    if (count < 2) {
                        tvMinus.setAnimation(getHiddenAnimation());
                        tvMinus.setVisibility(View.GONE);
                        tvCount.setVisibility(View.GONE);
                    }
                    count--;
                    activity.remove(item, false);//activity.getSelectedItemCountById(item.id)
                    tvCount.setText(String.valueOf(count));

                }
                break;
                default:
                    break;
            }
        }
    }

    private Animation getShowAnimation(){
        AnimationSet set = new AnimationSet(true);
        RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        set.addAnimation(rotate);
        TranslateAnimation translate = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF,2f
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0);
        set.addAnimation(translate);
        AlphaAnimation alpha = new AlphaAnimation(0,1);
        set.addAnimation(alpha);
        set.setDuration(500);
        return set;
    }

    private Animation getHiddenAnimation(){
        AnimationSet set = new AnimationSet(true);
        RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        set.addAnimation(rotate);
        TranslateAnimation translate = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,2f
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0);
        set.addAnimation(translate);
        AlphaAnimation alpha = new AlphaAnimation(1,0);
        set.addAnimation(alpha);
        set.setDuration(500);
        return set;
    }
}
